module.exports = function(grunt) {

  var buildPath = 'target/static-as/';

  var sourcePath = {
    css: buildPath + 'css/',
    js: buildPath + 'js/'
  };

  var targetPath = {
    raw: buildPath + 'raw/',
    min: buildPath + 'min/'
  };

  // Project configuration.
  grunt.initConfig({
    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
    banner: '',
    // Task configuration.
    less: {
      general: {
        options: {
          paths: sourcePath.css
        },
        src: [
          sourcePath.css + 'h5bp-top.less',
          sourcePath.css + 'grid.less',
          sourcePath.css + 'general.less',
          sourcePath.css + 'elements.less',
          sourcePath.css + 'pages.less',
          sourcePath.css + 'h5bp-bottom.less',
          sourcePath.css + 'mq.less'
        ],
        dest: sourcePath.css + '_less-combined.css'
      },
      ie8: {
        options: {
          paths: sourcePath.css
        },
        src: sourcePath.css + 'fuck-ie8.less',
        dest: targetPath.raw + 'fix-ie8.css'
      },
      styleguide: {
        options: {
          paths: sourcePath.css
        },
        src: sourcePath.css + 'styleguide.less',
        dest: targetPath.raw + 'styleguide.css'
      }
    },

    concat: {
      css: {
        src: [
          sourcePath.css + 'normalize.css',
          sourcePath.css + '_less-combined.css',
          sourcePath.css + 'adplayer.css'
        ],
        dest: targetPath.raw + 'default-final.css',
        nonull: true
      },
      // Libraries from (3rd-party) vendors
      jsHead: {
        src: [
          sourcePath.js + 'vendor/modernizr-2.6.2.min.js',
          sourcePath.js + 'vendor/jquery-1.9.1.min.js',
          sourcePath.js + 'vendor/jquery-migrate-1.2.1.min.js',
          sourcePath.js + 'vendor/jquery.ba-hashchange.min.js',
          sourcePath.js + 'vendor/jquery.scrollTo.min.js',
          sourcePath.js + 'vendor/jquery.hammer.min.js',
          sourcePath.js + '_js-head-more.js'
        ],
        dest: targetPath.raw + 'lib-head-final.js',
        nonull: true
      },
      jsBodyEnd: {
        src: [ // hint these files!
          sourcePath.js + 'helpers.js',
          sourcePath.js + 'plugins.js', // General (smaller) modules
          sourcePath.js + 'hero.js', // Hero element
          sourcePath.js + 'slideshow.js', // Slideshow app
          sourcePath.js + 'unmask-links.js', // Link masking
          sourcePath.js + 'hyphenat.js', // added soft hyphens to Teaser
          sourcePath.js + 'ready.js' // document ready...
        ],
        dest: targetPath.raw + 'lib-body-end-final.js'
      },
      polyfills: {
        src: [
          sourcePath.js + 'fuck-ie8.js'
        ],
        dest: targetPath.raw + 'polyfills.js'
      },
      ie8css: {
        src: [
          targetPath.raw + '_default-final-ie8.css',
          targetPath.raw + 'fix-ie8.css'
        ],
        dest: targetPath.raw + 'default-final-ie8.css'
      }
    },

    jshint: {
      options: {
        undef: true,
        // some vars defined in lib-head...
        globals: {
          jQuery: false,
          Modernizr: false,
          window: true,
          console: true,
          document: true,
          $ui: true,
          ui: true,
          Hammer: true,
          Konami: true
        }
      },
      jsBodyEnd: {
        src: [
          sourcePath.js + 'plugins.js', // General (smaller) modules
          sourcePath.js + 'hero.js', // Hero element
          sourcePath.js + 'slideshow.js', // Slideshow app
          sourcePath.js + 'ready.js' // document ready...
        ]
      }
    },

    jasmine: {
      options: {
        vendor: [
          targetPath.min + 'lib-head-final.js',
          sourcePath.js + 'vendor/jasmine-jquery.js'
        ]
      },
      hero: {
        options: {
          helpers: sourcePath.js + 'test/heroHelper.js',
          specs: sourcePath.js + 'test/heroSpec.js'
        },
        src: sourcePath.js + 'hero.js'
      },
      slideshow: {

      }
    },

    cssmin: {
      general: {
        src: targetPath.raw + 'default-final.css',
        dest: targetPath.min + 'default-final.css'
      },
      ie8: {
        src: targetPath.raw + 'default-final-ie8.css',
        dest: targetPath.min + 'fix-ie8.css'
      }
    },

    uglify: {
      // some 3rd party libs are not minified
      jsHead: {
        src: [
          sourcePath.js + 'vendor/jquery.cookie.js',
          sourcePath.js + 'vendor/base64v1_0.js',
          sourcePath.js + 'vendor/adplayer.js',
          sourcePath.js + 'vendor/matchmedia.js',
          sourcePath.js + 'vendor/picturefill.js',
          sourcePath.js + 'vendor/konami.js',
          sourcePath.js + 'adservice-adition.js',
          sourcePath.js + 'ui-init.js'
        ],
        dest: sourcePath.js + '_js-head-more.js'
      },
      jsBodyEnd: {
        src: [
          targetPath.raw + 'lib-body-end-final.js'
        ],
        dest: targetPath.min + 'lib-body-end-final.js'
      }
    },

    // Process CSS for IE8
    comment_mq: {
      ie8: {
        options: {},
        src: targetPath.raw + 'default-final.css',
        dest: targetPath.raw + '_default-final-ie8.css'
      }
    },

    rem2px: {
      ie8: {
        options: {
          basePx: 10
        },
        src: targetPath.raw + '_default-final-ie8.css',
        dest: targetPath.raw + '_default-final-ie8.css'
      }
    },

    clean: {
      build: [
        sourcePath.css + '_*.css',
        targetPath.raw + '_*.css',
        sourcePath.js + '_*.js'
      ]
    },

    watch: {
      development: {
        files: [
          sourcePath.css + '*.less',
          sourcePath.js + '*.js',
          '!' + sourcePath.js + '_*.js'
        ],
        tasks: [
          'prototype'
        ]
      }
    }
  });

  // Self-backed plugins
  grunt.loadTasks('custom_tasks');

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-jasmine');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-clean');

  // Default task.
  grunt.registerTask('default', [
    'less',
    'concat:css',
    'comment_mq',
    'rem2px',
    'concat:ie8css',
    'cssmin',
    'uglify:jsHead',
    'concat:jsHead',
    'concat:jsBodyEnd',
    'concat:polyfills',
    'jshint',
    'uglify:jsBodyEnd',
    'clean'
  ]);

  // Tasks for Prototype
  grunt.registerTask('prototype', [
    'less',
    'concat:css',
    'uglify:jsHead',
    'concat:jsHead',
    'concat:jsBodyEnd',
    'concat:polyfills',
    'jshint',
    'clean'
  ]);
};
